package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：yangjian
 * @date ：Created in 2019/7/5
 * @description ：
 * @version: 1.0
 */

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    //增加角色
    @Override
    public void add(Role role, Integer[] pIds, Integer[] mIds) {
        roleDao.add(role);
        for (Integer pId : pIds) {
            Map map = new HashMap();
            map.put("role_id",role.getId());
            map.put("permission_id",pId);
            roleDao.addRoleAndPermission(map);
        }
        for (Integer mId : mIds) {
            Map map = new HashMap();
            map.put("role_id",role.getId());
            map.put("menu_id",mId);
            roleDao.addRoleAndMenu(map);
        }
    }
    //查询权限列表
    @Override
    public List<Permission> findAllPermission() {
        return roleDao.findAllPermission();
    }

    //查询菜单列表
    @Override
    public List<Menu> findAllMenu() {
        return roleDao.findAllMenu();
    }

    @Override
    public Role findRoleById(Integer id) {
        return roleDao.findRoleById(id);
    }

    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        return roleDao.findPermissionIdsByRoleId(id);
    }

    @Override
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        return roleDao.findMenuIdsByRoleId(id);
    }






    //编辑
    @Transactional
    @Override
    public void edit(Role role, Integer[] pIds, Integer[] mIds) {
        roleDao.edit(role);
        roleDao.deletePermissionByRoleId(role.getId());
        for (Integer pId : pIds) {
            System.out.println();
            Map map = new HashMap();
            map.put("role_id",role.getId());
            map.put("permission_id",pId);
            roleDao.addRoleAndPermission(map);
        }
        roleDao.deleteMenuByRoleId(role.getId());
        for (Integer mId : mIds) {
            System.out.println();
            Map map = new HashMap();
            map.put("role_id",role.getId());
            map.put("menu_id",mId);
            roleDao.addRoleAndMenu(map);
        }
    }





    @Transactional
    @Override
    public void deleteById(Integer roleId) {

        //直接删除角色和菜单中间表中的关系
        roleDao.deleteRelWithMenu(roleId);
        //直接删除角色和权限中间表中的关系
        roleDao.deleteRelWithPermission(roleId);

        roleDao.deleteById(roleId);
    }


    /**
     *
     */


    //分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Role> page = roleDao.selectByCondition(queryPageBean.getQueryString());
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }
}
